package db

import (
	"database-platform/dal"
	"database-platform/logger"
	"database-platform/model"
	"fmt"
	"time"
)

func ListClusterRecords(cluster *model.Cluster, page, pageSize int64, search string) ([]*model.Cluster, uint64, error) {
	var query = dal.GORM
	var records []*model.Cluster
	var count uint64

	if cluster.Name != "" {
		query = query.Where("name = ?", cluster.Name)
	}
	if cluster.DBType != "" {
		query = query.Where("db_type = ?", cluster.DBType)
	}
	if cluster.BusinessOne != "" {
		query = query.Where("business_one = ?", cluster.BusinessOne)
	}
	if cluster.BusinessTwo != "" {
		query = query.Where("business_two = ?", cluster.BusinessTwo)
	}
	if cluster.Level != "" {
		query = query.Where("level = ?", cluster.Level)
	}
	if cluster.ClusterType != "" {
		query = query.Where("cluster_type = ?", cluster.ClusterType)
	}
	if cluster.Env != "" {
		query = query.Where("env = ?", cluster.Env)
	}
	if cluster.Unit != "" {
		query = query.Where("unit = ?", cluster.Unit)
	}
	if cluster.Status != "" {
		query = query.Where("status = ?", cluster.Status)
	}

	if search != "" {
		query = query.Where("name like ?", "%"+search+"%").
			Or("business_one like ?", "%"+search+"%").
			Or("business_two like ?", "%"+search+"%")
	}

	if page > 0 && pageSize > 0 {
		offset := (page - 1) * pageSize
		query = query.Offset(offset).Limit(pageSize)
	}

	err := query.Find(&records).Count(&count).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.cluster.ListClusterRecords sql execute err, err message is %s, cluster: %v", err, cluster)
		logger.Logger.Error(msg)
		return nil, 0, err
	}
	return records, count, nil
}

func GetClusterRecordById(id int) (record *model.Cluster, err error) {
	record = &model.Cluster{}
	tx := dal.GORM.Where("id = ?", id).First(&record)
	if tx.Error != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.cluster.GetClusterRecordById.sql.execute.err.message.%s.id.%v", tx.Error, id)
		logger.Logger.Error(msg)
		return nil, tx.Error
	}

	return record, nil
}

// InsertClusterRecord 插入集群记录
func InsertClusterRecord(cluster *model.Cluster) error {
	cluster.UpdateTime = time.Now()
	cluster.CreateTime = time.Now()
	err := dal.GORM.Create(cluster).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.cluster.InsertClusterRecord sql execute err, err message is %s, cluster: %v", err, cluster)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}

// UpdateClusterRecord 更新集群记录
func UpdateClusterRecord(id int, cluster *model.Cluster) error {
	cluster.UpdateTime = time.Now()
	cluster.ID = int64(id)
	err := dal.GORM.Save(cluster).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.cluster.UpdateClusterRecord sql execute err, err message is %s, cluster: %v", err, cluster)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}

// DeleteClusterRecord 删除集群记录
func DeleteClusterRecord(id int) error {
	err := dal.GORM.Delete(&model.Cluster{}, id).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.cluster.DeleteClusterRecord sql execute err, err message is %s, id: %d", err, id)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}
